/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/


#include "trialtask.h"

#include "atc.h"
#include "engine/engine.h"

#include <qlayout.h>
//Added by qt3to4:
#include <Q3VBoxLayout>
#include <QShowEvent>


using namespace pact;


//------------------------------------------------------------------------------
// class constants
//

const char* TrialTask::MAIN_TARGET = "main_event_target";


//------------------------------------------------------------------------------
// construction/destruction
//

/*!
 */
TrialTask::TrialTask(pact::TrialTaskType a_type)
	  : Task()
	  , _engine(0)
	  , m_type(a_type)
{
	_engine = create_engine(m_type);
	connect( _engine, SIGNAL( conflicts() ), this, SIGNAL( conflicts() ) );

	connect( this, SIGNAL( started() ), this, SIGNAL( signal_start() ) );
	connect( this, SIGNAL( closed() ), this, SIGNAL( signal_end() ) );
}

/*!
 */
TrialTask::~TrialTask() {
	destroy_engine( _engine );
	emit signal_end();
}

//------------------------------------------------------------------------------

/*!
 */
void TrialTask::initialize() {
	_engine->init();

	QWidget* view = _engine->create_view( this, MAIN_TARGET );
	Q3VBoxLayout* layout = new Q3VBoxLayout( this );
	layout->addWidget( view );
}

/*!
 *
 */
void TrialTask::addTerminationTime( int secs ) {
	_engine->set_alarm( secs * atc::ONE_SECOND, this, SLOT( terminate() ) );
}

/*!
 *
 */
void TrialTask::set_timer( int ms, QObject *receiver, const char *slot ) {
	_engine->set_alarm( ms, receiver, slot );
}


//------------------------------------------------------------------------------
// [protected] virtual
//

/*!
 */
void TrialTask::showEvent( QShowEvent* e ) {
	Task::showEvent( e );
	_engine->start();
	emit started();
}

//------------------------------------------------------------------------------
//

/*!
 * Pause/Resume trial task
 */
void TrialTask::pause()  { _engine->stop(); emit signal_pause(); }
void TrialTask::resume() { _engine->start(); emit signal_resume(); }

////////////////////////////////////////////////////////////////////////////////
